package com.tys.algorithm.beginner;

public class Code3_random {
    public static void main(String[] args) {
        int testTimes = 1000000;
        //在[0-1)区间，random函数0-0.95的概率
        random0_1(testTimes, 0.95);
        //在[0-8)区间，random函数0-6的概率 6/8
        random0_8(testTimes, 6);
        //在[0-k)区间，random函数0-k的概率 6/8
        random0_k(8, testTimes, 6);
        //在[0 - k-1]区间，random函数0-k的概率 6/9
        random0_k_(9, testTimes, 6);
        //在[0-1)区间，random函数0.7平方的概率
        random_x2(testTimes, 0.17);
        random_x2_min(testTimes, 0.17);


    }

    //在[0-1)区间
    public static void random0_1(int testTimes, double num) {
        System.out.println("======================");
        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (Math.random() < num) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
    }

    //在[0-8)区间
    public static void random0_8(int testTimes, double num) {
        System.out.println("======================");

        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (Math.random() * 8 < num) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
    }

    //在[0-k)区间
    public static void random0_k(int k, int testTimes, double num) {
        System.out.println("======================");

        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (Math.random() * k < num) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
    }

    //在[0-k]区间
    public static void random0_k_(int k, int testTimes, double num) {
        System.out.println("======================");

        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if ((int) (Math.random() * k) < num) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
        System.out.println((double) num / (double) k);


        int[] counts = new int[9];
        for (int i = 0; i < testTimes; i++) {
            int ans = (int) (Math.random() * k);
            counts[ans]++;
        }

        for (int i = 0; i < k; i++) {
            System.out.println(i + "这个数出现了" + counts[i] + "次");
        }
    }

    //概率调整为 x^2
    public static void random_x2(int testTimes, double num) {
        System.out.println("======================");

        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (xToPower2() < num) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
        System.out.println(Math.pow(num, 2));
    }

    //概率调整为 1-(1-x)^2
    public static void random_x2_min(int testTimes, double num) {
        System.out.println("======================");

        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (xToPower2_min() < num) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
        System.out.println((double) 1 - Math.pow((double) (1 - num), 2));
    }

    //取大的
    public static double xToPower2() {
        return Math.max(Math.random(), Math.random());
    }

    //取大的
    public static double xToPower3() {
        return Math.max(Math.random(), Math.max(Math.random(), Math.random()));
    }

    //取小的
    public static double xToPower2_min() {
        return Math.min(Math.random(), Math.random());
    }
}
